package com.woniuxy.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.commons.entity.Goods;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhangyong
 * @Date 2021/8/2 0002
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private RestTemplate restTemplate;

    private String url = "http://PROVIDER/goods/";
    //RestTemplate

    @HystrixCommand(fallbackMethod = "abc")
    @GetMapping("/find")
    public List<Goods> findOrderById(){
        //发送请求得到数据
        List forObject = restTemplate.getForObject(url+"all", List.class);
        //
        return forObject;
    }

    public List<Goods> abc(){
        //发送请求得到数据
        List<Goods> goodsList = new ArrayList<>();
        //
        return goodsList;
    }

    @GetMapping("/id")
    public Goods find(){
        Goods goods = restTemplate.getForObject(url + "find/1004", Goods.class);
        return goods;
    }

    @GetMapping("/add")
    public Boolean add(){
        Goods goods = new Goods(1005,"键盘",100);
        //通过post请求传递对象
        //参数2：提交的数据 参数3：返回值类型
        //发送的是json格式
        Boolean result = restTemplate.postForObject(url + "add",goods, boolean.class);
        //
        return result;
    }

    @GetMapping("/update")
    public Boolean update(){
        Goods goods = new Goods(1006,"耳机",190);
        //put 请求 没有返回值
        restTemplate.put(url+"update",goods);
        //
        return true;
    }

    @GetMapping("/del")
    public void del(){
        //del没有返回值
        restTemplate.delete(url+"del/1007");
    }
}
